<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Goods;
use Illuminate\Http\Request;

use App\Http\Requests\User\GoodsRequest;
use App\Http\Resources\GoodsResource;
use Cache;



class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){
        $goodslist = auth()->user()->goods()->paginate(10);


        return view('user.goods.goodslist' , compact('goodslist'));
    }

    /**
     * 商品浏览分析
     *
     * @return \Illuminate\Http\Response
     */
    public function goodsBrowse(){


        return view('user.goods.goodsBrowse');
    }

    /**
     * 商品浏览数据
     *
     * @return \Illuminate\Http\Response
     */
    public function goodsBrowseData(Request $request , Goods $goods){

        list($limit , $where , $verify , $exclude) = $goods->SearchHandler($request);

        $response = $goods->sumBrowseTotal($exclude)
            ->where('user_id' , auth()->id())
            ->when($verify , function($query) use ($where){

            $query->where($where);
        })->paginate($limit);

        return new GoodsResource($goods->requestLayuiData($response));
    }


    /**
     * 添加商品页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $goods = '';
        return view('user.goods.add',compact('goods'));
    }

    /**
     * 创建商品
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(GoodsRequest $request , Goods $goods)
    {
        if (auth()->user()->goods()->count() >= 5){
            return response()->json([
                'code'=> 0,
                'msg' => '最多添加5个商品',
            ]);
        }
        $goods->fill($request->all());
        $goods->user()->associate(auth()->user());

        if($goods->save()){
            return response()->json([
                'code'=> 1,
                'msg' => '添加成功',
            ]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function show(Goods $good)
    {

        return view('user.goods.show',compact('good'));
    }

    public function show_data(Request $request , Goods $goods)
    {
        //dd(auth()->id() , $goods);
        $this->authorize('update' , $goods);

        // 生成查询条件 ，
        list($limit , $where , $verify , $exclude) = $goods->SearchHandler($request);

        $getCatch = Cache::remember('goodsBrowseData' , '10' , function() use ($goods , $exclude){
            return $goods->sumShopIdGoodsBrowseTrait($exclude)->InRandomOrder()->with('shop','shop.shopType')->paginate(1000);
        });

        return $goods->requestLayuiData($getCatch);
    }


    /**
     * 修改商品信息页面
     *
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function edit(Goods $good)
    {
        $goods = $good;

        return view('user.goods.add' , compact('goods'));
    }

    /**
     * 修改商品信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Goods $good)
    {
        $good->fill($request->all());
        if($good->save()){
            return response()->json([
                'code'=> 1,
                'msg' => '修改成功',
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function destroy(Goods $good)
    {
        $this->authorize('update' , $good);

//        if ($good->delete()){
            return response()->json([
                'code'=> 0,
                'msg' => '无权限',
            ]);
//        }
        //
    }
}
